package com.hdaccp.goodsprovider.controller;

import com.hdaccp.goodsprovider.dao.GoodsDao;
import com.hdaccp.goodsprovider.service.GoodsService;
import org.main.bean.POJO.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Id;
import java.util.List;

@RestController
@RequestMapping("/goodsinfo")
public class GoodsController {

    @Autowired
    private GoodsDao goodsDao;

@Autowired
    private GoodsService goodsService;

    @RequestMapping("/queryPage/{currentPage}/{pageSize}")
    public List<PGoods>queryPage(@PathVariable("currentPage") int currentPage,@PathVariable("pageSize") int pageSize){
        return goodsService.queryPage(currentPage,pageSize);
    }

    @RequestMapping("/query")
    public List<PGoods>query(){
        return goodsDao.findAll();
    }


    @RequestMapping("/getOne/{id}")
    public PGoods getOne(@PathVariable("id") int id){

        return goodsDao.getOne(id);
    }



    @RequestMapping("/save/{colorimgId}/{typeId}/{brandId}/{sizeId}/{merchantsId}/{goods}/{price}/{images}/{sales}/{inventory}/{spstte}/{fold}/{houfold}")
    @ResponseBody
    public String save(@PathVariable("colorimgId") int colorimgId,@PathVariable("typeId") int typeId,@PathVariable("brandId") int brandId,@PathVariable("sizeId") int sizeId,@PathVariable("merchantsId") int merchantsId,@PathVariable("goods") String goods,@PathVariable("price") String price,@PathVariable("images") String images,@PathVariable("sales") int sales,@PathVariable("inventory") int inventory,@PathVariable("spstte") int spstte,@PathVariable("fold") String fold,@PathVariable("houfold") String houfold) {
        String msg = "success";
        PGcolorimg pGcolorimg = new PGcolorimg(colorimgId);
        PType pType = new PType(typeId);
        PGoodsType pGoodsType = new PGoodsType(brandId);
        PMerchants pMerchants = new PMerchants(merchantsId);
        PGsize pGsize = new PGsize(sizeId);
        PGoods pGoods = new PGoods(pGcolorimg,pType,pGoodsType,pGsize,pMerchants,goods,price,images,sales,inventory,spstte,fold,houfold);

        pGcolorimg.setId(colorimgId);
//        pGoods.setPGcolorimg(pGcolorimg);

//        pType.setId(typeId);
//        pGoods.setPType(pType);

//        pGoodsType.setId(brandId);
//        pGoods.setPGoodsType(pGoodsType);

//        pGsize.setId(sizeId);
//        pGoods.setPGsize(pGsize);

//        pMerchants.setId(merchantsId);
//        pGoods.setPMerchants(pMerchants);
//        pGoods.setGoods(goods);
//        pGoods.setPrice(price);
//        pGoods.setImages(images);
//        pGoods.setSales(sales);
//        pGoods.setInventory(inventory);
//        pGoods.setSpstte(spstte);
//        pGoods.setFold(fold);
//        pGoods.setHoufold(houFlod);

        try {
            goodsDao.save(pGoods);
        } catch (Exception e) {
            msg = "shibai";
        }
        return msg;


    }

    @RequestMapping("/update/{id}/{colorimgId}/{typeId}/{brandId}/{sizeId}/{merchantsId}/{goods}/{price}/{images}/{sales}/{inventory}/{spstte}/{fold}/{houFlod}")
    @ResponseBody
    public String update(@PathVariable("id") int id,@PathVariable("colorimgId") int colorimgId,@PathVariable("typeId") int typeId,@PathVariable("brandId") int brandId,@PathVariable("sizeId") int sizeId,@PathVariable("merchantsId") int merchantsId,@PathVariable("goods") String goods,@PathVariable("price") String price,@PathVariable("images") String images,@PathVariable("sales") int sales,@PathVariable("inventory") int inventory,@PathVariable("spstte") int spstte,@PathVariable("fold") String fold,@PathVariable("houFlod") String houFlod) {
     String msg="success";

//     PGcolorimg pGcolorimg=new PGcolorimg(colorimgId);
//     PType pType=new PType(typeId);
//     PGoodsType pGoodsType=new PGoodsType(brandId);
//     PGsize pGsize=new PGsize(sizeId);
//     PMerchants pMerchants=new PMerchants(merchantsId);
//        PGoods pGoods1 = goodsDao.findById(id).get();
//        pGoods1 = new PGoods(pGcolorimg,pType,pGoodsType,pGsize,pMerchants,goods,price,images,sales,inventory,spstte,fold,houfold);*/

        PGoods pGoods=goodsDao.getOne(id);
        PGcolorimg pGcolorimg = new PGcolorimg();
        pGcolorimg.setId(colorimgId);
        pGoods.setPGcolorimg(pGcolorimg);
        PType pType = new PType();
        pType.setId(typeId);
        pGoods.setPType(pType);
        PGoodsType pGoodsType = new PGoodsType();
        pGoodsType.setId(brandId);
        pGoods.setPGoodsType(pGoodsType);
        PGsize pGsize = new PGsize();
        pGsize.setId(sizeId);
        pGoods.setPGsize(pGsize);
        PMerchants pMerchants = new PMerchants();
        pMerchants.setId(merchantsId);
        pGoods.setPMerchants(pMerchants);
        pGoods.setGoods(goods);
        pGoods.setPrice(price);
        pGoods.setImages(images);
        pGoods.setSales(sales);
        pGoods.setInventory(inventory);
        pGoods.setSpstte(spstte);
        pGoods.setFold(fold);
        pGoods.setHoufold(houFlod);

        try {
            goodsDao.save(pGoods);
        } catch (Exception e) {
            msg = "shibai";
        }
        return msg;


    }



    @RequestMapping("/del/{id}")
   public String del(@PathVariable("id") int id){
        String msg="删除商品成功";
        try {
            goodsDao.delete(id);
        }catch(Exception e){
            msg="删除商品失败";
        }
       return msg;
   }





}
